//
//  LoginViewController.m
//  newDouban
//
//  Created by dllo on 15/10/17.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "UserViewController.h"
#import "DataBaseSingTon.h"

@interface LoginViewController ()

@property (nonatomic, retain) UITextField *userName;
@property (nonatomic, retain) UITextField *passWord;
@property (nonatomic, retain) UIButton *login;
@property (nonatomic, retain) UIButton *registerB;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"用户登录";
    
    
    
    UILabel *userNameL = [[UILabel alloc] initWithFrame:CGRectMake(20, 50 + 70, 80, 50)];
    userNameL.text = @"用户名:";
    userNameL.font = [UIFont boldSystemFontOfSize:24.0];
    [self.view addSubview:userNameL];
    [userNameL release];
    
    
    UILabel *passWordL = [[UILabel alloc] initWithFrame:CGRectMake(20, 50 + 50 + 20 + 70, 80, 50)];
    passWordL.text = @"密码:";
    passWordL.font = [UIFont boldSystemFontOfSize:24.0];
    [self.view addSubview:passWordL];
    [passWordL release];
    
    
    
    self.userName = [[UITextField alloc] initWithFrame:CGRectMake(20 + 80 + 25, 50 + 70, 270, 50)];
    self.userName.placeholder = @"请输入用户名";
    self.userName.borderStyle = UITextBorderStyleRoundedRect;
    self.userName.font = [UIFont boldSystemFontOfSize:25.0];
    [self.view addSubview:self.userName];
    [_userName release];
    
    self.passWord = [[UITextField alloc] initWithFrame:CGRectMake(20 + 80 + 25, 50 + 50 + 20 + 70, 270, 50)];
    self.passWord.placeholder = @"请输入密码";
    self.passWord.borderStyle = UITextBorderStyleRoundedRect;
    self.passWord.font = [UIFont boldSystemFontOfSize:25.0];
    [self.view addSubview:self.passWord];
    [_passWord release];
    
    
    self.login = [UIButton buttonWithType:UIButtonTypeCustom];
    self.login.frame = CGRectMake(25, 200 + 70, 150, 50);
    [self.login setTitle:@"登陆" forState:UIControlStateNormal];
    self.login.titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
    [self.login addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.login.backgroundColor = [UIColor colorWithRed:148 / 255.0 green:189 / 255.0 blue:68 / 255.0 alpha:1.0];
    [self.view addSubview:self.login];
    
    self.registerB = [UIButton buttonWithType:UIButtonTypeCustom];
    self.registerB.frame = CGRectMake(230, 200 + 70, 150, 50);
    [self.registerB setTitle:@"注册" forState:UIControlStateNormal];
    self.registerB.titleLabel.font = [UIFont boldSystemFontOfSize:24.0];
    [self.registerB addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    self.registerB.backgroundColor = [UIColor colorWithRed:215 / 255.0 green:127 / 255.0 blue:52 / 255.0 alpha:1.0];
    [self.view addSubview:self.registerB];
    
    
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)] autorelease];
    

    //***********************************************************
    
    
    
}

- (void)UIAlertController
{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码不正确" preferredStyle:UIAlertControllerStyleAlert];
    //注意:假如下面要加textfield要用UIAlertControllerStyleAlert
    
    [self presentViewController:alertC animated:YES completion:^{
        
        
    }];
    
    //创建提示按钮
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //block回调
        NSLog(@"点击的是确定按钮");
        
    }];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //block回调
        NSLog(@"点击的是取消按钮");
        
    }];
    
    //将按钮放在提示框上
    [alertC addAction:action];
    [alertC addAction:action1];
}



- (void)btnAction:(UIButton *)btn
{
    if (btn == self.login) {
        NSString *sql = [NSString stringWithFormat:@"SELECT * FROM user"];
        NSMutableArray *arrM = [[DataBaseSingTon shareDataBase] selectUserSQL:sql];
        BOOL result1 = [arrM containsObject:self.userName.text];
        
        if (result1) {
            
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:self.userName.text forKey:@"userName1"];
            [userDefaults synchronize];

            [self dismissViewControllerAnimated:YES completion:^{
               
            }];
            
        } else {
            [self UIAlertController];
        }
    }
    if (btn == self.registerB) {
        RegisterViewController *registerVC = [[RegisterViewController alloc] init];
        [self.navigationController pushViewController:registerVC animated:YES];
        [registerVC release];
    }
}

- (void)leftBarButtonAction:(UIBarButtonItem *)sender
{
    //[self.navigationController popViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
